<?php
class ControllerSaleThongKeDaCap extends Controller {
	private $error = array();

	public function index() {
	$this->language->load('sale/customer');
		$this->data['token'] = $this->session->data['token'];
		$this->document->setTitle('Thống kê dữ liệu');
		$this->template = 'sale/scan_customer.tpl';	
		$this->children = array(
			'common/footer',
			'common/header'	
		);
		$this->response->setOutput($this->render());
	}
	//Thong ke he thong
	public function scanBySystem($data){
		$this->load->model('sale/customer');
        $data['filter_status'] = 1;
        $data['filter_truycap'] = 1;
        unset($data['customer_id']);
        $customers = $this->model_sale_customer->getCustomersForScan($data);
        $total = 0;
        $this->data['customers'] = array();
        if($customers){
            foreach ($customers as $result) {
                $total += (int)$result['price'];
                $price2 = $this->model_sale_customer->getHistoriesPrice2($result['customer_id']);
                if($price2){
                    $total += (int)$price2['price'];
                }
                $customer_parent_info = $this->model_sale_customer->getCustomerForHH($result['customer_parent']);
                $this->data['customers'][] = array(
                    'customer_id'    => $result['customer_id'],
                    'madn'           => $result['madn'],
                    'price'          => $result['price'],
                    'price2'         =>$price2 ?  $this->currency->format($price2['price']) : 0,
                    'parent'         =>$customer_parent_info ? $customer_parent_info['madn'] : '',
                    'name'           => $result['name'],
                    'email'          => $result['email'],
                    'status_id'      => $result['status'],
                    'status'         => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
                    'date_added'     => date($this->language->get('date_format_short'), strtotime($result['date_added']))
                );
            }
        }
        $this->data['total'] = $this->currency->format($total);
        $this->template = 'sale/form_scanBySystem.tpl';

        $this->response->setOutput($this->render());
	}
	
	public function scanByCustomerID($data){
		$this->load->model('sale/customer');
        $data['filter_status'] = 1;
        $data['filter_truycap'] = 1;
        $customers = $this->dacap->dfs($data['customer_id']);
		print_r($customers);exit;
        $total = 0;
        $this->data['customers'] = array();
        if($customers){
            foreach ($customers as $result) {
                $total += (int)$result['price'];
                $price2 = $this->model_sale_customer->getHistoriesPrice2($result['customer_id']);
                if($price2){
                    $total += (int)$price2['price'];
                }
                $customer_parent_info = $this->model_sale_customer->getCustomerForHH($result['customer_parent']);
                $this->data['customers'][] = array(
                    'customer_id'    => $result['customer_id'],
                    'madn'           => $result['madn'],
                    'price'          => $result['price'],
                    'price2'         =>$price2 ?  $this->currency->format($price2['price']) : 0,
                    'parent'         =>$customer_parent_info ? $customer_parent_info['madn'] : '',
                    'name'           => $result['name'],
                    'email'          => $result['email'],
                    'status_id'      => $result['status'],
                    'status'         => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
                    'date_added'     => date($this->language->get('date_format_short'), strtotime($result['date_added']))
                );
            }
        }
        $this->data['total'] = $this->currency->format($total);
        $this->template = 'sale/form_scanBySystem.tpl';

        $this->response->setOutput($this->render());
	}
	
	public function scanCustomer() {
		//Input: customer_id, date_start, date_end
		if($this->request->get['filter_type_scan'] == 1){
			$this->scanBySystem($this->request->get);
		} else {
			$this->scanByCustomerID($this->request->get);
		}
	}

    public function activeTransaction() {
        $this->language->load('sale/customer');
        $this->load->model('sale/customer');
        $results = $this->model_sale_customer->getCustomer($this->request->get['customer_id']);
        if($results){
            $this->model_sale_customer->addTransactionsCustomerForLevel($this->request->get['customer_id'], $this->request->get['comment']);

            $this->session->data['success'] = 'Hệ thống thanh toán thành công cho thành viên ' . $results['madn'];
        } else {
            $this->session->data['warning'] = 'Vui long kiểm tra lại, thành viên không tồn tại trong hệ thống';
        }
        $this->redirect($this->url->link('sale/customer', 'token=' . $this->session->data['token'], 'SSL'));
    }
}
?>